{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# python小技巧笔记\n",
    "[TOC]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字典\n",
    "字典类似最基本的key-value的json串，形成类似数组或者类的形式。下面是一个最基本的例子。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "defaultdict(<class 'int'>, {'Peter': 4, 'Piper': 4, 'picked': 3, 'a': 2, 'peck': 4, 'of': 4, 'pickled': 4, 'peppers.': 1, 'A': 1, 'peppers': 2, 'picked.If': 1, 'peppers,': 1, \"Where's\": 1, 'the': 1, '?': 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "\n",
    "sentence= \"Peter Piper picked a peck of pickled peppers.\\\n",
    "    A peck of pickled peppers Peter Piper picked.If Peter Piper picked a peck of pickled peppers, \\\n",
    "    Where's the peck of pickled peppers Peter Piper picked ?\"\n",
    "\n",
    "word_dict=defaultdict(int)\n",
    "\n",
    "for word in sentence.split():\n",
    "    word_dict[word]+=1\n",
    "\n",
    "print(word_dict) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({'Peter': 4, 'Piper': 4, 'peck': 4, 'of': 4, 'pickled': 4, 'picked': 3, 'a': 2, 'peppers': 2, 'peppers.': 1, 'A': 1, 'picked.If': 1, 'peppers,': 1, \"Where's\": 1, 'the': 1, '?': 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "sentence= \"Peter Piper picked a peck of pickled peppers.\\\n",
    "    A peck of pickled peppers Peter Piper picked.If Peter Piper picked a peck of pickled peppers, \\\n",
    "    Where's the peck of pickled peppers Peter Piper picked ?\"\n",
    "\n",
    "words=sentence.split()\n",
    "\n",
    "word_count=Counter(words)\n",
    "\n",
    "print(word_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元组\n",
    "顺序型数据结构，不可变，只有in/not in，比较串联切片索引，最大最小的操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "c\n",
      "2\n",
      "tuple change error\n",
      "(1, 2, 'c', [1, 2, 3])\n",
      "(1, 2, 'c', [1, 10, 3])\n",
      "(1, 2, 'a', 1, 2, 'c', [1, 10, 3])\n",
      "(2, 3, 4, 5, 6, 7, 8, 9, 0)\n",
      "9 0\n",
      "10 is not in a_tuple\n"
     ]
    }
   ],
   "source": [
    "# 创建\n",
    "a_tuple=(1,2,'a')\n",
    "b_tuple=1,2,'c'\n",
    "\n",
    "# 访问\n",
    "print(a_tuple[0])\n",
    "print(b_tuple[-1])\n",
    "print(b_tuple[-2])\n",
    "\n",
    "# 不可修改性\n",
    "try: \n",
    "    b_tuple[0]=20\n",
    "except:\n",
    "    print('tuple change error')\n",
    "    \n",
    "# 元组不可变，但是元组内部的数组可变\n",
    "c_tuple=1,2,'c',[1,2,3]\n",
    "print(c_tuple)\n",
    "c_tuple[3][1]=10\n",
    "print(c_tuple)\n",
    "\n",
    "# 串联\n",
    "print(a_tuple+c_tuple)\n",
    "\n",
    "# 切片\n",
    "d_tuple=(1,2,3,4,5,6,7,8,9,0)\n",
    "print(d_tuple[1:])\n",
    "\n",
    "# 最大最小值\n",
    "print(max(d_tuple),min(d_tuple))\n",
    "\n",
    "# in & not in \n",
    "if 10 in a_tuple:\n",
    "    print('10 is in a_tuple')\n",
    "else:\n",
    "     print('10 is not in a_tuple')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 集合\n",
    "不可重复，支持交集、并集、差集、对称差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "5\n",
      "{'dogs', 'hate', 'cats'}\n",
      "3\n",
      "{'of', 'A', 'a', 'peppers.', 'hate', 'Piper', 'the', 'cat', 'dogs', 'dog', 'Peter', 'peppers,', 'cats', 'peppers', \"Where's\", 'picked.If', '?', 'picked', 'pickled', 'peck'}\n",
      "20\n",
      "0.15\n"
     ]
    }
   ],
   "source": [
    "st_1=\"Peter Piper picked a peck of pickled peppers.\\\n",
    "    A peck of pickled peppers Peter Piper picked.If Peter Piper picked a peck of pickled peppers, \\\n",
    "    Where's the peck of pickled peppers Peter Piper picked ? \\\n",
    "    dogs hate cats\"\n",
    "st_2='dogs hate cats dog dog dog cat'\n",
    "\n",
    "# 创建集合\n",
    "st_1_wrds=set(st_1.split())\n",
    "st_2_wrds=set(st_2.split())\n",
    "\n",
    "# 集合大小\n",
    "no_wrds_st_1=len(st_1_wrds)\n",
    "print(no_wrds_st_1)\n",
    "no_wrds_st_2=len(st_2_wrds)\n",
    "print(no_wrds_st_2)\n",
    "\n",
    "# 集合交集\n",
    "cmn_w=st_1_wrds.intersection(st_2_wrds)\n",
    "print(cmn_w)\n",
    "no_cmn_wrds=len(cmn_w)\n",
    "print(no_cmn_wrds)\n",
    "\n",
    "# 集合并集\n",
    "unq_w=st_1_wrds.union(st_2_wrds)\n",
    "print(unq_w)\n",
    "no_unq_wrds=len(unq_w)\n",
    "print(no_unq_wrds)\n",
    "\n",
    "# Jaccard相似度\n",
    "similarity =no_cmn_wrds/(no_unq_wrds)\n",
    "print(similarity)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表\n",
    "可以进行操作，，进行修改，栈和队列都能从这里实现。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3]\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3]\n",
      "[5, 6, 7, 8]\n",
      "3\n",
      "2\n",
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "[19, 15, 13, 14, 4, 12, 5, 16, 7, 18, 17, 11, 10, 6, 8, 2, 1, 3, 9]\n",
      "None\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
      "None\n",
      "[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "# 创建列表\n",
    "a=list(range(1,10))\n",
    "b=[1,2,3]\n",
    "print(a,b)\n",
    "\n",
    "# 可切片可串联等和集合类似的功能都能实现\n",
    "print(a+b)\n",
    "print(a[-5:-1])\n",
    "\n",
    "# 栈\n",
    "a_stack=[]\n",
    "\n",
    "a_stack.append(1)\n",
    "a_stack.append(2)\n",
    "a_stack.append(3)\n",
    "\n",
    "print(a_stack.pop())\n",
    "print(a_stack.pop())\n",
    "print(a_stack.pop())\n",
    "\n",
    "# 队列\n",
    "# 栈\n",
    "a_queue=[]\n",
    "\n",
    "a_queue.append(1)\n",
    "a_queue.append(2)\n",
    "a_queue.append(3)\n",
    "\n",
    "print(a_queue.pop(0))\n",
    "print(a_queue.pop(0))\n",
    "print(a_queue.pop(0))\n",
    "\n",
    "# 排序和反转\n",
    "from random import shuffle\n",
    "a=list(range(1,20))\n",
    "\n",
    "shuffle(a)\n",
    "print(a)\n",
    "\n",
    "a.sort()\n",
    "print(a)\n",
    "\n",
    "a.reverse()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 列表生成器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 4, 9, 16, 25, 36, 49, 64]\n"
     ]
    }
   ],
   "source": [
    "a=[-1,2,3,-4,5,-6,7,-8]\n",
    "b=[pow(x,2) for x in a if x<0]\n",
    "print(b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
